# ===================================================================
# API Test Automation Script (PowerShell Version)
# Function: Run pytest tests quickly
# ===================================================================

Write-Host "========================================" -ForegroundColor Green
Write-Host "       API Test Automation Script" -ForegroundColor Green
Write-Host "========================================" -ForegroundColor Green
Write-Host ""

# Create report directories
if (!(Test-Path "reports")) {
    New-Item -ItemType Directory -Path "reports" | Out-Null
    Write-Host "[INFO] Created reports directory" -ForegroundColor Yellow
}

if (!(Test-Path "allure-results")) {
    New-Item -ItemType Directory -Path "allure-results" | Out-Null
    Write-Host "[INFO] Created allure-results directory" -ForegroundColor Yellow
}

Write-Host "[INFO] Starting tests..." -ForegroundColor Cyan
Write-Host ""

# Run pytest tests with Allure
try {
    $testResult = pytest -v --tb=short --alluredir=allure-results
    $exitCode = $LASTEXITCODE
    
    Write-Host ""
    Write-Host "[INFO] Generating Allure report..." -ForegroundColor Cyan
    
    # Generate Allure report
    allure generate allure-results -o allure-report --clean
    
    # Check test results and open Allure report
    if ($exitCode -eq 0) {
        Write-Host ""
        Write-Host "[SUCCESS] All tests passed!" -ForegroundColor Green
        Write-Host "[INFO] Allure report generated: allure-report\index.html" -ForegroundColor Yellow
        Write-Host "[INFO] Opening Allure report..." -ForegroundColor Yellow
        Start-Process "allure-report\index.html"
    } else {
        Write-Host ""
        Write-Host "[WARNING] Some tests failed!" -ForegroundColor Red
        Write-Host "[INFO] Allure report generated: allure-report\index.html" -ForegroundColor Yellow
        Write-Host "[INFO] Opening Allure report..." -ForegroundColor Yellow
        Start-Process "allure-report\index.html"
    }
} catch {
    Write-Host "[ERROR] Failed to run tests: $($_.Exception.Message)" -ForegroundColor Red
    exit 1
}

Write-Host ""
Write-Host "Press any key to exit..." -ForegroundColor Gray
$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")